home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / gblanker3.5.src.lha / GSource / Blankers / Life / blank.c next >
Encoding:
C/C++ Source or Header  |  1994-10-08  |  8.9 KB  |  377 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11.  
  12. #include <dos/dos.h>
  13.  
  14. #include <clib/exec_protos.h>
  15. #include <clib/intuition_protos.h>
  16. #include <clib/graphics_protos.h>
  17. #include <clib/alib_protos.h>
  18.  
  19. #include "/Garshnelib/Garshnelib_protos.h"
  20. #include "/Garshnelib/Garshnelib_pragmas.h"
  21.  
  22. #include "Life.h"
  23. #include "//defs.h"
  24. #include "/main.h"
  25.  
  26. #ifdef _M68020
  27. #define PREFOBJ LONG
  28. #define UPREFOBJ ULONG
  29. #else
  30. #define PREFOBJ WORD
  31. #define UPREFOBJ UWORD
  32. #endif
  33.  
  34. #define HSIZE ( sizeof( PREFOBJ ) * 8 )
  35. #define HCELLS ( 2 * HSIZE )
  36.  
  37. struct ModulePrefs
  38. {
  39.     LONG Mode;
  40.     LONG Depth;
  41.     LONG Delay;
  42.     LONG Generations;
  43.     LONG Density;
  44. };
  45.  
  46. extern struct ModulePrefs nP;
  47.  
  48. PREFOBJ *Org[2], *Orgs[2], *Lookup;
  49. LONG uw, x1pos, x2pos, ypos, offx, BWid, Hei;
  50. Triplet *ColorTable = 0L;
  51.  
  52. #define RAND( base, offset ) (( ULONG )( RangeRand( base ) + offset ))
  53. #define ORG( v, x, y ) ( Org[v][Lookup[y]+x] )
  54. #define VALUE (( i < HSIZE )? 0L : 1L )
  55.  
  56. UBYTE mask[] = { 96, 240, 240, 96 };
  57. #define RectFill( r, x1, y1, x2, y2 ) BltPattern( r, 0L, x1, y1, x2, y2, 0 )
  58.  
  59. VOID SpewRandomShit( PREFOBJ *Org[], PREFOBJ *Orgs[], LONG v )
  60. {
  61.     LONG x = RAND( HCELLS-4, 2 ), y = RAND( Hei-4, 2 ), i, j;
  62.  
  63.     for( j = y; j < y+3; j++ )
  64.         for( i = x; i < x+3; i++ )
  65.             if( RAND( 100, 0 ) > 25 )
  66.             {
  67.                 ORG( v, i, j ) = 1;
  68.                 if( i < HSIZE )
  69.                     Orgs[v][2*j] |= ( 1L << ( (HSIZE-1) - i ));
  70.                 else
  71.                     Orgs[v][2*j+1] |= ( 1L << ( ( HSIZE - 1 ) - i ));
  72.             }
  73. }
  74.  
  75. VOID FillWithRandomShit( struct RastPort *r, PREFOBJ *Org[], PREFOBJ *Orgs[],
  76.                         LONG v, LONG Density )
  77. {
  78.     LONG i, i1, j;
  79.     UPREFOBJ flag;
  80.     
  81.     SetAPen( r, 2L );
  82.  
  83.     for( ypos = 0, j = 0; j < Hei; j++, ypos += uw )
  84.     {
  85.         for( x1pos = offx, x2pos = offx+(uw*HSIZE), i = 0, i1 = HSIZE,
  86.             flag = 1L << (HSIZE-1);    i < HSIZE; i++, i1++, x1pos += uw,
  87.             x2pos += uw, flag = ( 1L << (HSIZE-1) - i ))
  88.         {
  89.             if( RangeRand( 100 ) < Density )
  90.             {
  91.                 ORG( v, i, j ) = 1;
  92.                 Orgs[v][2*j] |= flag;
  93.                 RectFill( r, x1pos, ypos, x1pos + BWid, ypos + BWid );
  94.             }
  95.             if( RangeRand( 100 ) < Density )
  96.             {
  97.                 ORG( v, i1, j ) = 1;
  98.                 Orgs[v][2*j+1] |= flag;
  99.                 RectFill( r, x2pos, ypos, x2pos + BWid, ypos + BWid );
  100.             }
  101.         }
  102.     }
  103. }
  104.  
  105. #define TOP (Hei*2-2)
  106. VOID Grow( PREFOBJ *Orgs, PREFOBJ *OrgsN )
  107. {
  108.     LONG i, j;
  109.     UPREFOBJ mask = 1L << (HSIZE-1), one = 1;
  110.     
  111.     CopyMemQuick( Orgs, OrgsN, 2 * Hei * sizeof( PREFOBJ ));
  112.  
  113.     if( Orgs[0] & one )
  114.         OrgsN[1] |= mask;
  115.     Orgs[0] |= ( Orgs[0] >> 1 );
  116.         if( Orgs[1] & one )
  117.             OrgsN[0] |= mask;
  118.     Orgs[1] |= ( Orgs[1] >> 1 );
  119.     if( Orgs[0] & mask )
  120.         OrgsN[1] |= one;
  121.     Orgs[0] |= ( Orgs[0] << 1 );
  122.     if( Orgs[1] & mask )
  123.         OrgsN[0] |= one;
  124.     OrgsN[1] |= ( Orgs[1] << 1 );
  125.     OrgsN[0] |= Orgs[TOP];
  126.     OrgsN[0] |= Orgs[2];
  127.     OrgsN[1] |= Orgs[TOP+1];
  128.     OrgsN[1] |= Orgs[3];
  129.  
  130.     for( i = 2, j = 3; i < TOP; i += 2, j += 2 )
  131.     {
  132.         OrgsN[i] |= ( Orgs[i] >> 1 );
  133.         OrgsN[j] |= ( Orgs[j] >> 1 );
  134.         OrgsN[i] |= ( Orgs[i] << 1 );
  135.         OrgsN[j] |= ( Orgs[j] << 1 );
  136.         if( Orgs[i] & one )
  137.             OrgsN[j] |= mask;
  138.         if( Orgs[j] & one )
  139.             OrgsN[i] |= mask;
  140.         if( Orgs[i] & mask )
  141.             OrgsN[j] |= one;
  142.         if( Orgs[j] & mask )
  143.             OrgsN[i] |= one;
  144.         OrgsN[i] |= Orgs[i-2];
  145.         OrgsN[i] |= Orgs[i+2];
  146.         OrgsN[j] |= Orgs[j-2];
  147.         OrgsN[j] |= Orgs[j+2];
  148.     }
  149.  
  150.     if( Orgs[TOP] & one )
  151.         OrgsN[TOP+1] |= mask;
  152.     OrgsN[TOP] |= ( Orgs[TOP] >> 1 );
  153.     if( Orgs[TOP+1] & one )
  154.         OrgsN[TOP] |= mask;
  155.     OrgsN[TOP+1] |= ( Orgs[TOP+1] >> 1 );
  156.     if( Orgs[TOP] & mask )
  157.         OrgsN[TOP+1] |= one;
  158.     OrgsN[TOP] |= ( Orgs[TOP] << 1 );
  159.     if( Orgs[TOP+1] & mask )
  160.         OrgsN[TOP] |= one;
  161.     OrgsN[TOP+1] |= ( Orgs[TOP+1] << 1 );
  162.     OrgsN[TOP] |= Orgs[TOP-2];
  163.     OrgsN[TOP] |= Orgs[0];
  164.     OrgsN[TOP+1] |= Orgs[TOP-1];
  165.     OrgsN[TOP+1] |= Orgs[1];
  166. }
  167.  
  168. LONG Blank( VOID *Prefs )
  169. {
  170.     struct ModulePrefs *lP;
  171.     struct Screen *LScr;
  172.     struct Window *Wnd;
  173.     struct RastPort *r;
  174.     LONG Gens, RetVal = OK, colrand, maxcol, iter = 1, nbrs, n = 0, m = 1;
  175.     LONG tofront = 0, i, i1, gi, li, j, gj, lj;
  176.     UPREFOBJ flag;
  177.     
  178.     if( LifeWnd )
  179.         lP = &nP;
  180.     else
  181.         lP = ( struct ModulePrefs * )Prefs;
  182.     
  183.     colrand = ( 1L << lP->Depth ) - 1;
  184.     if( lP->Depth > 2 )
  185.         maxcol = colrand - (1L<<lP->Depth)/6;
  186.     else
  187.         maxcol = colrand;
  188.     
  189.     Gens = lP->Generations;
  190.     
  191.     LScr = OpenScreenTags( 0l, SA_DisplayID, lP->Mode, SA_Depth, lP->Depth,
  192.                           SA_Quiet, TRUE, SA_Behind, TRUE,
  193.                           SA_Overscan, OSCAN_TEXT, TAG_DONE );
  194.     if( LScr )
  195.     {
  196.         r = &( LScr->RastPort );
  197.         uw = LScr->Width / HCELLS;
  198.         offx = ( LScr->Width - ( uw * HCELLS ))/ 2;
  199.         Hei = LScr->Height / uw;
  200.         BWid = uw-2;
  201.         
  202.         Org[0] = AllocVec( sizeof( PREFOBJ ) * HCELLS * Hei, MEMF_CLEAR );
  203.         Org[1] = AllocVec( sizeof( PREFOBJ ) * HCELLS * Hei, MEMF_CLEAR );
  204.         Orgs[0] = AllocVec( sizeof( PREFOBJ ) * 2 * Hei, MEMF_CLEAR );
  205.         Orgs[1] = AllocVec( sizeof( PREFOBJ ) * 2 * Hei, MEMF_CLEAR );
  206.         Lookup = AllocVec( sizeof( PREFOBJ ) * Hei, MEMF_CLEAR );
  207.         
  208.         if( Org[0] && Org[1] && Orgs[0] && Orgs[1] && Lookup )
  209.         {
  210.             SetRGB4(&( LScr->ViewPort ), 0, 0, 0, 0 );
  211.             if( lP->Depth > 1 )
  212.                 ColorTable = RainbowPalette( LScr, 0L, 1L, 0L );
  213.             else
  214.                 SetRGB4(&( LScr->ViewPort ), 1, 0xF, 0xF, 0xF );
  215.             Wnd = BlankMousePointer( LScr );
  216.             ScreenToFront( LScr );
  217.             
  218.             for( i = 1; i < Hei; i++ )
  219.                 Lookup[i] = Lookup[i-1] + HCELLS;
  220.             
  221.             FillWithRandomShit( r, Org, Orgs, n, lP->Density );
  222.             
  223.             while( RetVal == OK )
  224.             {
  225.                 WaitTOF();
  226.  
  227.                 if(!( tofront++ % 60 ))
  228.                     ScreenToFront( LScr );
  229.  
  230.                 if( !lP->Delay || !( tofront % lP->Delay ))
  231.                 {
  232.                     if( !iter )
  233.                         SpewRandomShit( Org, Orgs, n );
  234.                     
  235.                     Grow( Orgs[n], Orgs[m] );
  236.  
  237.                     CopyMemQuick( Org[n], Org[m],
  238.                                  HCELLS * Hei * sizeof( PREFOBJ ));
  239.                     
  240.                     for( ypos = 0, j = 0; j < Hei; j++, ypos += uw )
  241.                     {
  242.                         for( x1pos = offx, x2pos = offx+(uw*HSIZE), i = 0,
  243.                             i1 = HSIZE; i < HSIZE;
  244.                             i++, i1++, x1pos += uw, x2pos += uw )
  245.                         {
  246.                             flag = ( UPREFOBJ )( 1L << ( HSIZE - 1 - i ));
  247.                             if( Orgs[m][2*j] & flag )
  248.                             {
  249.                                 gi = i + 1;
  250.                                 li = ( i + HCELLS - 1 )%HCELLS;
  251.                                 gj = ( j + 1 )%Hei;
  252.                                 lj = ( j + Hei - 1 )%Hei;
  253.                                 nbrs = 0;
  254.                                 if( ORG( n, gi, gj )) nbrs++;
  255.                                 if( ORG( n, i, gj )) nbrs++;
  256.                                 if( ORG( n, li, gj )) nbrs++;
  257.                                 if( ORG( n, gi, j )) nbrs++;
  258.                                 if( ORG( n, li, j )) nbrs++;
  259.                                 if( ORG( n, gi, lj )) nbrs++;
  260.                                 if( ORG( n, i, lj )) nbrs++;
  261.                                 if( ORG( n, li, lj )) nbrs++;
  262.                                 switch( nbrs )
  263.                                 {
  264.                                 case 3:
  265.                                     if( ORG( m, i, j ) < maxcol ) {
  266.                                         SetAPen( r, ( LONG )++ORG( m, i, j ));
  267.                                         RectFill( r, x1pos, ypos, x1pos + BWid,
  268.                                                  ypos + BWid );
  269.                                     } break;
  270.                                 case 2:
  271.                                     if( ORG( m, i, j )) {
  272.                                         if( ORG( m, i, j ) < maxcol ) {
  273.                                             SetAPen( r,
  274.                                                     ( LONG )++ORG( m, i, j ));
  275.                                             RectFill( r, x1pos, ypos, x1pos +
  276.                                                      BWid, ypos + BWid );
  277.                                         }
  278.                                     } else
  279.                                         Orgs[m][2*j] &= ~flag;
  280.                                     break;
  281.                                 default:
  282.                                     ORG( m, i, j ) = 0;
  283.                                     Orgs[m][2*j] &= ~flag;
  284.                                     if( ORG( n, i, j ))    {
  285.                                         SetAPen( r, 0L );
  286.                                         RectFill( r, x1pos, ypos, x1pos + BWid,
  287.                                                  ypos + BWid );
  288.                                     }
  289.                                 }
  290.                             }
  291.                             else
  292.                                 ORG( m, i, j ) = 0;
  293.                             if( Orgs[m][2*j+1] & flag )
  294.                             {
  295.                                 gi = ( i1 + 1 )%HCELLS;
  296.                                 li = ( i1 - 1 )%HCELLS;
  297.                                 gj = ( j + 1 )%Hei;
  298.                                 lj = ( j + Hei - 1 )%Hei;
  299.                                 nbrs = 0;
  300.                                 if( ORG( n, gi, gj )) nbrs++;
  301.                                 if( ORG( n, i1, gj )) nbrs++;
  302.                                 if( ORG( n, li, gj )) nbrs++;
  303.                                 if( ORG( n, gi, j )) nbrs++;
  304.                                 if( ORG( n, li, j )) nbrs++;
  305.                                 if( ORG( n, gi, lj )) nbrs++;
  306.                                 if( ORG( n, i1, lj )) nbrs++;
  307.                                 if( ORG( n, li, lj )) nbrs++;
  308.                                 switch( nbrs )
  309.                                 {
  310.                                 case 3:
  311.                                     if( ORG( m, i1, j ) < maxcol )
  312.                                     {
  313.                                         SetAPen( r, ( LONG )++ORG( m, i1, j ));
  314.                                         RectFill( r, x2pos, ypos, x2pos + BWid,
  315.                                                  ypos + BWid );
  316.                                     } break;
  317.                                 case 2:
  318.                                     if( ORG( m, i1, j ))
  319.                                     {
  320.                                         if( ORG( m, i1, j ) < maxcol )
  321.                                         {
  322.                                             SetAPen( r,
  323.                                                     ( LONG )++ORG( m, i1, j ));
  324.                                             RectFill( r, x2pos, ypos, x2pos +
  325.                                                      BWid, ypos + BWid );
  326.                                         }
  327.                                     }
  328.                                     else
  329.                                     Orgs[m][2*j+1] &= ~flag;
  330.                                     break;
  331.                                 default:
  332.                                     ORG( m, i1, j ) = 0;
  333.                                     Orgs[m][2*j+1] &= ~flag;
  334.                                     if( ORG( n, i1, j ))
  335.                                     {
  336.                                         SetAPen( r, 0L );
  337.                                         RectFill( r, x2pos, ypos, x2pos + BWid,
  338.                                                  ypos + BWid );
  339.                                     }
  340.                                 }
  341.                             }
  342.                             else
  343.                                 ORG( m, i1, j ) = 0;
  344.                         }
  345.                     }
  346.                     
  347.                     n = m;
  348.                     m = 1 - m;
  349.                     iter = (iter+1) % Gens;
  350.                 }
  351.                 RetVal = ContinueBlanking();
  352.             }
  353.             if( lP->Depth )
  354.                 RainbowPalette( 0L, ColorTable, 1L, 0L );
  355.             UnblankMousePointer( Wnd );
  356.             CloseScreen( LScr );
  357.         }
  358.         else
  359.             RetVal = FAILED;
  360.  
  361.         if( Org[0] )
  362.             FreeVec( Org[0] );
  363.         if( Org[1] )
  364.             FreeVec( Org[1] );
  365.         if( Orgs[0] )
  366.             FreeVec( Orgs[0] );
  367.         if( Orgs[1] )
  368.             FreeVec( Orgs[1] );
  369.         if( Lookup )
  370.             FreeVec( Lookup );
  371.     }
  372.     else
  373.         RetVal = FAILED;
  374.     
  375.     return RetVal;
  376. }
  377.